package com.xuzao.developer.controller;

import com.github.pagehelper.PageInfo;
import com.xuzao.developer.annotation.MethodAnnotation;
import com.xuzao.developer.annotation.ModuleAnnotation;
import com.xuzao.developer.base.model.GridTable;
import com.xuzao.developer.dto.GridTableDTO;
import com.xuzao.developer.service.GridTableService;
import com.xuzao.developer.tools.entity.Pager;
import com.xuzao.developer.utils.BJUIMap;
import com.xuzao.developer.vo.BaseResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author xuzao
 * @description
 * @date 2019-09-03 17:12:36
 **/
@Controller
@RequestMapping("/gridTable/")
@ModuleAnnotation(name = "表头")
public class GridTableController extends BaseController {

    @Resource
    private GridTableService gridTableService;


    @RequestMapping("queryList")
    public String queryList(GridTableDTO dto, Pager pager, Model model) {
        super.initPager(pager);

        PageInfo<GridTable> list = gridTableService.list(dto, pager);

        model.addAttribute("pageInfo", list);
        model.addAttribute("bo", dto);
        return "/grid_table/grid_table_list";
    }

    @MethodAnnotation(name = "新增")
    @RequestMapping("add")
    public String addGridTable(Model model) {
        model.addAttribute("bo", new GridTable());
        return "/grid_table/grid_table_add";
    }

    @MethodAnnotation(name = "修改")
    @RequestMapping("update")
    public String update(Model model, GridTableDTO gridTableDTO) {
        model.addAttribute("bo", gridTableService.get(gridTableDTO));
        return "/grid_table/grid_table_update";
    }

    @MethodAnnotation(name = "保存")
    @RequestMapping("save")
    @ResponseBody
    public BJUIMap save(GridTableDTO dto) {
        BJUIMap map = new BJUIMap();
        map.setMessage("操作成功");
        map.setTabid("gridTable");
        map.setCloseCurrent(BJUIMap.CALLBACK_CLOSE);
        map.setStatusCode(BJUIMap.CODE_OK);
        if (StringUtils.isNotBlank(dto.getFlag())) {
            gridTableService.insert(dto);
        } else {
            gridTableService.update(dto);
        }
        return map;
    }

    @MethodAnnotation(name = "删除")
    @RequestMapping("del")
    @ResponseBody
    public BJUIMap del(Long id) {
        BJUIMap map = new BJUIMap();
        map.setMessage("操作成功");
        map.setTabid("gridTable");
        map.setStatusCode(BJUIMap.CODE_OK);
        gridTableService.del(id);
        return map;
    }

    @RequestMapping("updateGridTable")
    public BaseResultVo updateGridTable(GridTableDTO dto) {
        return gridTableService.update(dto);
    }

}